package com.zmn.oms.services.impl.conf.distribute;

import com.zmn.base.common.data.dubbo.interfaces.area.AreaListRemoteService;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.Option;
import com.zmn.common.dto2.OptionData;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.model.entity.conf.distribute.DistributeWeightCompany;
import com.zmn.oms.model.entity.conf.distribute.DistributeWeightCompanyQuery;
import com.zmn.oms.model.entity.conf.distribute.DistributeWeightStatusQuery;
import com.zmn.oms.persistence.interfaces.conf.distribute.DistributeWeightCompanyDao;
import com.zmn.oms.services.interfaces.conf.distribute.DistributeWeightCompanyService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：派单参数服务商配置业务处理实现
 *
 * @author mujunlin
 * @version v1.0
 * @since 2019/10/17 16:13
 */
@Service
public class DistributeWeightCompanyServiceImpl implements DistributeWeightCompanyService {

    @Resource
    private DistributeWeightCompanyDao distributeWeightCompanyDao;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    protected AreaListRemoteService areaListRemoteService;

    @Override
    public DistributeWeightCompany findByKey(Integer weightId) {
        return distributeWeightCompanyDao.findByKey(weightId);
    }

    @Override
    public List<DistributeWeightCompany> listPageByQuery(DistributeWeightCompanyQuery query) {
        Integer count = distributeWeightCompanyDao.listPageCountByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : distributeWeightCompanyDao.listPageByQuery(query);
    }

    @Override
    public List<Option<Integer>> listCityByCompanyId(Integer companyId, Integer bizType) {
        List<VtDTO> vtDTOList = distributeWeightCompanyDao.listCityByCompanyId(companyId, bizType);
        if (CollectionUtils.isEmpty(vtDTOList)) {
            return new ArrayList<>();
        }
        List<Integer> collect = vtDTOList.stream().map(VtDTO::getValue).collect(Collectors.toList());
        ResponseDTO<OptionData<Integer>> listResponseDTO = areaListRemoteService.listOptionAreaByIds(collect);
        if (Objects.isNull(listResponseDTO.getData()) || CollectionUtils.isEmpty(listResponseDTO.getData().getOptions())) {
            return Collections.EMPTY_LIST;
        }
        List<Option<Integer>> options = listResponseDTO.getData().getOptions();
        return options;
    }

    @Override
    public Integer countByQuery(DistributeWeightCompanyQuery query) {
        return distributeWeightCompanyDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(DistributeWeightCompany distributeWeightCompany) {
        return distributeWeightCompanyDao.updateByKey(distributeWeightCompany);
    }

    @Override
    public Integer updateStatusByQuery(DistributeWeightStatusQuery query) {
        return distributeWeightCompanyDao.updateStatusByQuery(query);
    }

    @Override
    public Integer insert(DistributeWeightCompany distributeWeightCompany) {
        return distributeWeightCompanyDao.insert(distributeWeightCompany);
    }

    @Override
    public Integer deleteByKey(Integer weightId) {
        return distributeWeightCompanyDao.deleteByKey(weightId);
    }
}
